// packageK/merchants/merchants.js
// import drawQrcode from '../lib/weapp.qrcode.min'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    span: 12,
    QRcodeText: 'http://ww.baidu.com/',
    total_business: '',
    current_month_total_business: '',
    total_already_business: '',
    withdraw_able_amount: '-',
    career_type: null,
    mc_merchants_id: null,
    qrcodeSrc: ''
  },

  /**
   * 生成二维码
   */
  // generateQRcode: function() {
  //   drawQrcode({
  //     width: 200,
  //     height: 200,
  //     canvasId: "myQrcode",
  //     text: this.data.QRcodeText
  //   })
  // },

  /**
   * 获取二维码
   */
  getQrcode: function() {
    const _this = this
    const url = app.getNetAddresss('member.qrcode.getInviteQrCode')
    wx.showLoading({
      title: '加载中...',
    })
    app._getNetWork({
      url,
      success: function(res) {
        const resData = res.data
        if (resData.result == 1) {
          _this.setData({
            qrcodeSrc: resData.data.image_url
          })
        } else {
          app.tips(resData.msg)
        }
      },
      complete: function() {
        wx.hideLoading()
      }
    })
  },

  /**
   * 下载二维码
   */
  downloadQRcode: function() {
    if (!this.data.qrcodeSrc) {
      app.tips('没有可下载的二维码！')
      return
    }
    wx.showLoading({
      title: '加载中...',
    })
    // wx.downloadFile 方法：下载文件资源到本地
    wx.downloadFile({
      url: this.data.qrcodeSrc,
      success: function(downRes) {
        // wx.saveImageToPhotosAlbum方法：保存图片到系统相册
        wx.saveImageToPhotosAlbum({
          filePath: downRes.tempFilePath,
          success: function() {
            app.tips('保存成功')
          },
          fail: function(saveErr) {
            // 没有保存图片权限
            if (saveErr.errMsg === "saveImageToPhotosAlbum:fail:auth denied" || saveErr.errMsg === "saveImageToPhotosAlbum:fail auth deny" || saveErr.errMsg === "saveImageToPhotosAlbum:fail authorize no response") {
              wx.showModal({
                title: '提示',
                content: '需要您授权保存相册',
                complete: function(modRes) {
                  if (modRes.confirm) {
                    // 打开设置-开启保存图片权限
                    wx.openSetting({
                      success: function(settingRes) {
                        if (settingRes.authSetting['scope.writePhotosAlbum']) {
                          wx.showModal({
                            title: '提示',
                            content: '获取权限成功,再次点击下载即可保存二维码',
                            showCancel: false
                          })
                        } else {
                          wx.showModal({
                            title: '提示',
                            content: '获取权限失败，将无法保存到相册!',
                            showCancel: false
                          })
                        }
                      }
                    })
                  }
                }
              })
            }
          },
          complete: function() {
            wx.hideLoading()
          }
        })
      }
    })
  },

  /**
   * 获取统计数据
   */
  getStatisticalData: function() {
    const _this = this
    const url = app.getNetAddresss('platformApplet.statistics.get-merchants')
    wx.showLoading({
      title: '加载中...',
    })
    app._getNetWork({
      url,
      success: function(res) {
        const resData = res.data
        if (resData.result == 1) {
          const { total_business, current_month_total_business, total_already_business, withdraw_able_amount, career_type, mc_merchants_id } = resData.data
          _this.setData({
            total_business: total_business + '个',
            current_month_total_business: current_month_total_business + '个',
            total_already_business: total_already_business + '个',
            withdraw_able_amount: '¥' + withdraw_able_amount,
            career_type,
            mc_merchants_id
          })
          _this.getQrcode()
        } else {
          app.tips(resData.msg)
        }
      },
      complete: function() {
        wx.hideLoading()
        wx.stopPullDownRefresh()
      }
    })
  },

  /**
   * 提现
   */
  goWithdrawal: function() {
    wx.navigateTo({
      url: '/packageA/member/withdrawal/withdrawal',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getStatisticalData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    // this.generateQRcode()
    // this.getQrcode()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.getStatisticalData()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})